package com.sina.shop;

import com.sina.shop.config.*;
import com.sina.shop.util.Constant;
import com.sina.shop.web.resolver.JsonViewExceptionResolver;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.View;

import javax.servlet.ServletContext;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-09-21-18:37
 */
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@ServletComponentScan({"com.sina.shop.web.filter"})
@Import({ServiceConfig.class, CloudConfig.class, JsonViewConfig.class, CustomWebMvcConfigurerAdapter.class, RedisAutoConfig.class})
@EnableConfigurationProperties
@EnableAsync
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients
@EnableHystrixDashboard
public class WebApplication extends SpringBootServletInitializer implements ServletContextAware, ApplicationContextAware {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }

    private ServletContext servletContext;

    private ApplicationContext applicationContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    @Autowired
    public JsonViewExceptionResolver jsonViewExceptionResolver(
            @Qualifier("webLayerMessageSource") ResourceBundleMessageSource webLayerMessageSource,
            @Qualifier("commonView") View commonView) {
        JsonViewExceptionResolver jsonViewExceptionResolver = new JsonViewExceptionResolver();
        jsonViewExceptionResolver.setMessageSource(webLayerMessageSource);
        jsonViewExceptionResolver.setDefaultView(commonView);
        return jsonViewExceptionResolver;
    }

    @Bean
    public ResourceBundleMessageSource webLayerMessageSource(
            @Qualifier("serviceLayerMessageSource")
                    ResourceBundleMessageSource serviceLayerMessageSource) {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding(Constant.UTF8);
        resourceBundleMessageSource.setBasename("i18n/web/messages");
        resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
        resourceBundleMessageSource.setParentMessageSource(serviceLayerMessageSource);
        return resourceBundleMessageSource;
    }


    @Bean
    public MessageSourceAccessor defaultWebLayerMessageSourceAccessor(
            @Qualifier("webLayerMessageSource") ResourceBundleMessageSource webLayerMessageSource) {
        // 若有必要将来可 更改 messageSource
        return new MessageSourceAccessor(webLayerMessageSource, Locale.SIMPLIFIED_CHINESE);
    }
}
